// pages/curriculum_planning/curriculum_planning.js
var App = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        plan_list:[],//动作规划数组
        time:'',
        time1: "",
        time2: "",
        time3: "",
        crouse: "",
        name: "",
        corse_id: "",
        showtost: false,
        b_id:'',//课程的id,
        type: '', 
        start:'',  
        end: '',  
        membership_id: '', 
        startDate: '',
        endDate: '',
        see_details: '',//代表是私教课程的详情列表跳转的
        member_id:'',
        item_id:'',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var This= this;
        This.setData({
            b_id: options.b_id,
            type: options.type,
            start: options.start,
            end: options.end,
            startDate: options.startDate,
            endDate: options.endDate,
            membership_id: options.membership_id, 
            see_details: options.see_details,
            member_id: options.member_id,
            item_id:options.item_id
        })
        console.log(options.type)
        console.log(options.list)
        if (This.data.type == 2) {
            // console.log('编辑')
            // This.setData({
            //     b_id: options.b_id,
            //     type: options.type 
            // })
            console.log('aaa')
            This.loading();
        }else{
            // console.log('添加')
            if (options.list == undefined){
                console.log('bbb')
                This.setData({
                    plan_list: [
                        {
                            num: '第一组动作',
                            action_name: "",
                            action_time: '',
                            completion: '',
                            action_summary: '',
                            type: ''
                        },
                        {
                            num: '第二组动作',
                            action_name: "",
                            action_time: '',
                            completion: '',
                            action_summary: '',
                            type: ''
                        },
                        {
                            num: '第三组动作',
                            action_name: "",
                            action_time: '',
                            completion: '',
                            action_summary: '',
                            type: ''
                        },
                    ],
                    time1: options.time1,
                    time2: options.time2,
                    time3: options.time3,
                    crouse: options.crouse,
                    name: options.name,
                    // corse_id: options.id,
                    time: options.time1 + ' ' + options.time2 + '-' + options.time3,
                    b_id: options.b_id,
                    type: options.type
                })
            }else {
                console.log('sss')
                var a = decodeURIComponent(options.list);
                var b = JSON.parse(a)
                console.log(b)
                // var c = JSON.stringify(b)
                This.setData({
                    time1: options.time1,
                    time2: options.time2,
                    time3: options.time3,
                    crouse: options.crouse,
                    name: options.name,
                    // corse_id: options.id,
                    time: options.time1 + ' ' + options.time2 + '-' + options.time3,
                    b_id: options.b_id,
                    type: options.type,
                    time:options.time,
                    plan_list:b
                })
            }
            
        }
        // console.log(This.data.type)
        // console.log(This.data.b_id) 
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    loading:function(){
        var This = this;
        App._get('coach/Coursearrange/coursePlanningList', {
            openid: wx.getStorageSync('openid'),
            store_id: wx.getStorageSync('store_id'),
            phone: wx.getStorageSync('phone'),
            // openid: 1,
            // store_id: 53,
            // phone: 15612340001,
            b_id: This.data.b_id
        }, function (res) {
            if (res.status == 1) {
                This.setData({
                    plan_list: res.data.action_data,
                    time1: res.data.date_str,
                    time2: res.data.start_time,
                    time3: res.data.end_time,
                    name: res.data.user_name,
                    crouse: res.data.course_name,
                })
                This.setData({
                    time: This.data.time1 + ' ' + This.data.time2 + '-' + This.data.time3,
                })
            } else {
                wx.showToast({
                    title: res.error,
                    image: '../../image/icon_danger.png',
                    duration: 2000
                })
            }
        })
    },
    // 动作名称的修改
    actionName:function(e){
        var This= this;
        console.log(e.currentTarget.dataset.index)
        var index = e.currentTarget.dataset.index;
        var cartList = 'plan_list[' + index + '].action_name';
        This.setData({
            [cartList]: e.detail.value,
        });
        // console.log(This.data.plan_list)
    },
    // 时间的修改
    timeClick: function (e) {
        var This = this;
        // console.log(e.currentTarget.dataset.index)
        var index = e.currentTarget.dataset.index;
        var cartList = 'plan_list[' + index + '].action_time';
        This.setData({
            [cartList]: e.detail.value,
        });
        // console.log(This.data.plan_list)

    },
    // 完成情况的修改
    complaint: function (e) {
        var This = this;
        // console.log(e.currentTarget.dataset.index)
        var index = e.currentTarget.dataset.index;
        var cartList = 'plan_list[' + index + '].completion';
        This.setData({
            [cartList]: e.detail.value,
        });
        // console.log(This.data.plan_list)

    },
    // 总结的修改
    summary: function (e) {
        var This = this;
        // console.log(e.currentTarget.dataset.index)
        var index = e.currentTarget.dataset.index;
        var cartList = 'plan_list[' + index + '].action_summary';
        This.setData({
            [cartList]: e.detail.value,
        });
        // console.log(This.data.plan_list)

    },
    // 继续添加的事件
    add_list:function(){
        var This = this;
        var index = This.data.plan_list.length;
        var indexxx = This.data.plan_list.length-1;
        var ss = This.numberToUpperCase(index);
        var as = {
            num: '第'+ss+'组动作',
            action_name: "",
            action_time: '',
            completion: '',
            action_summary: '',
        }
        // var cs = This.data.plan_list.keys.push(as);
        var plist = 'plan_list[' + index + ']';
        This.setData({
            [plist]:as,
        })
        // console.log(This.data.plan_list)
    },
    // 删除动作
    detelt:function(e){
        var This = this;
        var index = This.data.plan_list.length-1;
        var indexxx = This.data.plan_list.length-2;
        var a = This.remove(This.data.plan_list, index)
        This.setData({
            plan_list:a 
        })
        // console.log(a)
    },
    // 下一步的点击事件
    step:function(){
        var This = this;
        This.setData({
            showtost: false,
        })
        console.log(This.data.plan_list)
        for (var index in This.data.plan_list){
            if (This.data.plan_list[index].action_name == '' || This.data.plan_list[index].action_time == '' || This.data.plan_list[index].completion == '' || This.data.plan_list[index].action_summary==''){
                This.setData({
                    showtost: true,
                })
            }
        }
        if (This.data.showtost == true){
            wx.showToast({
                title: '填写的内容不能为空',
                icon:'none'
            })
        }else {
            console.log(This.data.plan_list)
            var model = encodeURIComponent(JSON.stringify(This.data.plan_list));
            console.log(This.data.b_id)
            console.log(This.data.type)
            wx.navigateTo({
                url: '/pages/summary_training/summary_training?time=' + This.data.time + '&name=' + This.data.name + '&crouse=' + This.data.crouse + '&b_id=' + This.data.b_id + '&list=' + model + '&type=' + This.data.type + '&start=' + This.data.start + '&end=' + This.data.end + '&membership_id=' + This.data.membership_id + '&startDate=' + This.data.startDate + '&endDate=' + This.data.endDate + '&see_details=' + This.data.see_details + '&member_id=' + This.data.member_id + '&item_id=' + This.data.item_id,
            })
        }
    },
    // 删除数组的方法
    remove: function (array, val) {
        for (var i = 0; i < array.length; i++) {
            if (i == val) {
                array.splice(i, 1);
            }
        }
        return array;
    },
    // 数字转化成汉字
    numberToUpperCase:function(textIndex) {
        let newString = '';
        let newTextIndex = (textIndex + 1) + '';

        function sum(value, index) {
            var newValue = '';
            if((textIndex === 9)) {
            return !index ? '十' : '';
        }
        let isSeat = (~~textIndex > 9 && ~~textIndex < 19);
        switch (~~value) {
            case 1:
                newValue = !index ? (isSeat ? '' : '一') : '十一';
                break;
            case 2:
                newValue = !index ? (isSeat ? '' : '二') : '十二';
                break;
            case 3:
                newValue = !index ? (isSeat ? '' : '三') : '十三';
                break;
            case 4:
                newValue = !index ? (isSeat ? '' : '四') : '十四';
                break;
            case 5:
                newValue = !index ? (isSeat ? '' : '五') : '十五';
                break;
            case 6:
                newValue = !index ? (isSeat ? '' : '六') : '十六';
                break;
            case 7:
                newValue = !index ? (isSeat ? '' : '七') : '十七';
                break;
            case 8:
                newValue = !index ? (isSeat ? '' : '八') : '十八';
                break;
            case 9:
                newValue = !index ? (isSeat ? '' : '九') : '九十';
                break;
            case 0:
                newValue = '十';
                break;
            default:
                break;
        }
        return newValue;
            }

        for (let i = 0; i < newTextIndex.length; i++) {
            newString += sum(newTextIndex.substring(i, i + 1), i);
        }
        return newString;
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})